//
//  HYLWheelView.m
//  转轮实现
//
//  Created by hyl on 16/7/7.
//  Copyright © 2016年 hyl. All rights reserved.
//

#import "HYLWheelView.h"
#import "HYLButton.h"

@interface HYLWheelView ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation HYLWheelView

-(void)awakeFromNib{
    CGFloat btnW=68;
    CGFloat btnH=143;
    UIImage *image=[UIImage imageNamed:@"LuckyAstrology"];
    CGFloat scale=[UIScreen mainScreen].scale;
    CGFloat imageW=(image.size.width)/12*scale;
    CGFloat imageH=(image.size.height)*scale;
    //添加12按钮
    for (int i=0; i<12; i++) {
        HYLButton *btn=[HYLButton buttonWithType:UIButtonTypeCustom];
//        UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat h=self.bounds.size.width;
//        NSLog(@"%f",h);
        btn.layer.anchorPoint=CGPointMake(0.5, 1);
        btn.layer.position=CGPointMake(h*0.5, h*0.5);
        btn.bounds=CGRectMake(0, 0, btnW, btnH);
//        btn.frame=CGRectMake(0, 0, btnW, btnH);强制定位到了（0,0）
//        btn.backgroundColor=[UIColor redColor];
//        CABasicAnimation *basic=[CABasicAnimation animation];
//        basic.keyPath=@"transform.rotation";
//        basic.toValue=@(M_PI/6*(i+1));
//        [btn.layer addAnimation:basic forKey:nil];
        //旋转按钮
        btn.transform=CGAffineTransformMakeRotation((M_PI/6*(i+1)));
        
        //图片裁切大小
        CGRect clip=CGRectMake(imageW*i, 0, imageW , imageH);
        
        CGImageRef imageR=CGImageCreateWithImageInRect(image.CGImage, clip);
        //NSLog(@"%@   ---",NSStringFromCGSize(image.size));
        UIImage *image=[UIImage imageWithCGImage:imageR];
        [btn setImage:image forState:UIControlStateNormal];
        
//        self.imageView.image=(UIImage *)image;
        [self.imageView addSubview:btn];
    }
}

+(instancetype)wheelView{
    HYLWheelView *wheelView=[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].firstObject;
    
    return wheelView;
}
-(void)start{
    CABasicAnimation *basic=[CABasicAnimation animation];
    
    basic.keyPath=@"transform.rotation";
    basic.toValue=@(M_PI*2);
    basic.duration=3.5;
    basic.repeatCount=MAXFLOAT;
    
    [self.imageView.layer addAnimation:basic forKey:nil];
}

-(void)pause{

}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
